package com.study.thread;

import com.study.modules.saas.repository.model.ImageDO;

import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 测试线程异步任务
 *
 * @Author hupengming
 * @ClassName TestFutureTask
 * @Date 2023-05-19  11:30
 * @Version 1.0
 */
public class TestFutureTask {

    public static void m1() throws ExecutionException, InterruptedException {

        FutureTask<String> futureTask = new FutureTask<>(() -> {

            System.out.println("异步任务线程执行中......");

            return "hello call";

        });
        Thread t1 = new Thread(futureTask);

        t1.start();

        String s = futureTask.get();

        System.out.println(s);
    }

    public static void m2() throws ExecutionException, InterruptedException {
        FutureTask<ImageDO> futureTask = new FutureTask<>(() -> {
            System.out.println("异步任务线程执行中......");
            ImageDO imageDO = new ImageDO();
            imageDO.setId(UUID.randomUUID().variant());
            imageDO.setKey(UUID.randomUUID().toString());
            return imageDO;
        });

        Thread thread = new Thread(futureTask);
        thread.start();

        System.out.println("主线程执行中....");


        ImageDO imageDO = futureTask.get();
        System.out.println("image id:" + imageDO.getId() + " -- " + "image key:" + imageDO.getKey());
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        m2();
    }
}
